package com.jk.sec.service.Impl;

import com.jk.*;
import com.jk.sec.dao.SecBuyDao;
import com.jk.sec.service.SecBuyService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;

import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpSession;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class SecBuyServiceImpl implements SecBuyService {


    @Autowired
    private SecBuyDao secBuyDao;
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private RedisTemplate redisTemplate;
    //@Autowired
    //private HttpSession session;
    /**
     * @Author: 孙尔超
     * @Description: 影片详情
     * @Date: 2021/5/21 10:35
     * @param ids
     * @Return: com.jk.MovieBean
     **/
    @Override
    public MovieBean goupiao(Integer ids) {

        return secBuyDao.goupiao(ids);
    }
/**
 * @Author: 孙尔超
 * @Description: 写评语
 * @Date: 2021/5/21 15:10
 * @Return: com.jk.DiscussBean
 **/
    @Override
    public void discuss(String content,Integer ids,UserBean user,Integer star) {
        secBuyDao.star(ids,star);
        DiscussBean discuss = new DiscussBean();
        SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String format = sim.format(new Date());
        discuss.setDiscussdate(format);
        discuss.setName(user.getUserName());
        discuss.setGive(0);
        discuss.setMovieId(ids);
        discuss.setContent(content);
        discuss.setStar(star);
        mongoTemplate.save(discuss);
    }



    /**
     * @Author: 孙尔超
     * @Description: 查询所有评论
     * @Date: 2021/5/21 17:29
     * @Return: java.util.List<com.jk.DiscussBean>
     **/
    @Override
    public List<DiscussBean> findDiscuss(Integer ids) {
        Query query =new Query();
        query.addCriteria(Criteria.where("movieId").is(ids));
        query.with(Sort.by(Sort.Direction.DESC,"give"));
        List<DiscussBean> discussBeans = mongoTemplate.find(query, DiscussBean.class);
        return discussBeans;
    }
/**
 * @Author: 孙尔超
 * @Description: 点赞
 * @Date: 2021/5/21 17:29
 * @param ids
 * @Return: void
 **/
    @Override
    public void like(String ids,Integer give,String id,UserBean user) {
        Query query =new Query();//根据id进行修改点赞数量
        query.addCriteria(Criteria.where("id").is(id));//根据id进行修改点赞数量
        Update update = new Update();//根据id进行修改
        ServletRequestAttributes requestAttributes = ( ServletRequestAttributes ) RequestContextHolder.getRequestAttributes();
        HttpSession session = requestAttributes.getRequest().getSession();
        Integer userid = user.getUserId();//session的id
        Query query1 =new Query();
        query1.addCriteria(Criteria.where("userid").is(userid));
        query1.addCriteria(Criteria.where("discussid").is(id));
        List<GiveBean> giveBeans = mongoTemplate.find(query1, GiveBean.class);

        if (giveBeans.size()>0){//点过赞了
            update.set("give",give-1);//点过了数量减一
            mongoTemplate.remove(query1,GiveBean.class);//然后删除该条数据
        }
       else {//没点过赞
            GiveBean giveBean = new GiveBean();
            update.set("give",give+1);//点赞+1
            giveBean.setDiscussid(id);//给中间表存数据
            giveBean.setUserid(userid);
            mongoTemplate.save(giveBean);
        }

        mongoTemplate.upsert(query,update,DiscussBean.class);
    }

    /**
     * @Author: 孙尔超
     * @Description: 榜单查询
     * @Date: 2021/5/23 19:09
     * @Return: java.util.List<com.jk.MovieBean>
     **/
    @Override
    public List<MovieBean> monthlyFocus() {
        List<MovieBean> list=new ArrayList<MovieBean>();
        list= secBuyDao.monthlyFocus();
        return list;
    }
    /**
     * @Author: 孙尔超
     * @Description: 期望查询排序
     * @Date: 2021/5/25 15:18
     * @Return: java.util.List<com.jk.MovieBean>
     **/
    @Override
    public List<MovieBean> expect() {
        return secBuyDao.expect();
    }
/**
 * @Author: 孙尔超
 * @Description: 票房插叙排序
 * @Date: 2021/5/25 15:18
 * @Return: java.util.List<com.jk.MovieBean>
 **/
    @Override
    public List<MovieBean> boxoffice() {
        return secBuyDao.boxoffice();
    }
    /**
     * @Author: 孙尔超
     * @Description: 查询电影的所有演员
     * @Date: 2021/5/25 15:14
     * @param ids
     * @Return: java.util.List<com.jk.MovieBean>
     **/
    @Override
    public List<MovieBean> findActor(Integer ids) {
        return secBuyDao.findActor(ids);
    }

    @Override
    public Integer scorecount(Integer ids) {
        Query query=new Query();
        query.addCriteria(Criteria.where("movieId").is(ids));
        Integer count = (int) mongoTemplate.count(query, DiscussBean.class);
        return count;
    }
    /**
     * @Author: 孙尔超
     * @Description: 查询相关电影
     * @Date: 2021/6/6 21:06
     * @param typeid
     * @Return: java.util.List<com.jk.MovieBean>
     **/
    @Override
    public List<MovieBean> relatedMovie(String typeid) {
        String[] typeidsplit = typeid.split(",");
        return secBuyDao.relatedMovie(typeidsplit);
    }

    @Override
    public void show() throws ParseException {
        //查询所有电影的上映时间
        List<MovieBean> list=secBuyDao.show();
        //系统当前时间
        SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        for (MovieBean movie :list){
            String filmSchedule = movie.getFilmSchedule();
            String efilmSchedule = movie.getEfilmSchedule();
            Date parse = sim.parse(filmSchedule);
            Date efilm = sim.parse(efilmSchedule);
            if (date.getTime()>=parse.getTime()){
                secBuyDao.changeStatus(movie.getMovieId());
            }
            if (date.getTime()>=efilm.getTime()){
                secBuyDao.efilm(movie.getMovieId());
            }
        }

    }
    /**
     * @Author: 孙尔超
     * @Description: 查询平均评分
     * @Date: 2021/6/7 22:46
     * @param ids
     * @Return: java.lang.Double
     **/
    @Override
    public Double star(Integer ids) {
        MovieBean movie=secBuyDao.scoreStar(ids);
        Query query = new Query();
        query.addCriteria(Criteria.where("MovieId").is(ids));
        long count = mongoTemplate.count(query, DiscussBean.class);
        Double aDouble = Double.valueOf(count);
        Double aDouble1 = Double.valueOf(movie.getScore());
        Double i = aDouble1 / aDouble;
        DecimalFormat df=new DecimalFormat("0.0");
        String format = df.format(i);
        Double dou=Double.valueOf(format);
        if (dou!=0 && dou!=null){
            secBuyDao.updatestar(ids,dou);
        }
        return movie.getMovieGrade();
    }
    /**
     * @Author: 孙尔超
     * @Description: 想看
     * @Date: 2021/6/8 11:17
     * @param ids
     * @param user
     * @Return: com.jk.LookBean
     **/
    @Override
    public LookBean havaLook(Integer ids, UserBean user) {
        Query query = new Query();
        query.addCriteria(Criteria.where("userid").is(user.getUserId()));
        query.addCriteria(Criteria.where("movieId").is(ids));
        LookBean one = mongoTemplate.findOne(query, LookBean.class);
        return one;
    }
    /**
     * @Author: 孙尔超
     * @Description: 点击i想看
     * @Date: 2021/6/8 12:11
     * @param ids
     * @param user
     * @Return: void
     **/
    @Override
    public void loveLook(Integer ids, UserBean user) {
        secBuyDao.addLook(ids);
        LookBean lookBean = new LookBean();
        lookBean.setMovieId(ids);
        lookBean.setUserid(user.getUserId());
        mongoTemplate.save(lookBean);

    }
    /**
     * @Author: 孙尔超
     * @Description: 点击已想看
     * @Date: 2021/6/8 12:11
     * @param ids
     * @param user
     * @Return: void
     **/
    @Override
    public void loveLooks(Integer ids, UserBean user) {
        secBuyDao.updateLook(ids);
        Query query = new Query();
        query.addCriteria(Criteria.where("userid").is(user.getUserId()));
        query.addCriteria(Criteria.where("movieId").is(ids));
        mongoTemplate.remove(query,LookBean.class);
    }


}
